//
//  NearByBusinessViewController.m
//  XiJianChui
//
//  Created by aaa on 16/7/18.
//  Copyright © 2016年 FanShuHan. All rights reserved.
//

#import "NearByBusinessViewController.h"
#import "QhTitleButton.h"
#import "QhStoreCell.h"

static NSInteger const cols = 1;
static CGFloat const margin = 10;
static NSString * const ID = @"storeCell";
#define itemW (QhScreenW - 2 * margin) / cols
#define itemH 180
@interface NearByBusinessViewController ()

/** 标题栏 */
@property (nonatomic, weak) UIView *titlesView;
/** 标题下划线 */
@property (nonatomic, weak) UIView *titleUnderline;
/** 上一次点击的标题按钮 */
@property (nonatomic, weak) QhTitleButton *previousClickedTitleButton;
@end

@implementation NearByBusinessViewController

- (instancetype)init
{
    // 创建一个流水布局
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    // 设置cell的尺寸
    layout.itemSize = CGSizeMake(itemW, itemH);
    
    // 设置滚动的方向
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    // 设置cell之间的间距
    layout.minimumInteritemSpacing = margin;
    
    // 行间距
    layout.minimumLineSpacing = margin;
    return [super initWithCollectionViewLayout:layout];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //    self.navigationItem.rightBarButtonItem = [UIBarButtonItem backItemWithimage:[UIImage imageNamed:@"navigationButtonReturn"] highImage:[UIImage imageNamed:@"navigationButtonReturnClick"]  target:self action:@selector(nearByButtonClick:) title:@"附近 ^"];
    self.title = @"附近商家";
    [self createstylistCollectionView];
    [self createTitlesView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Create UI
- (void)createTitlesView
{
    UIView *titlesView = [[UIView alloc] init];
    titlesView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.5];
    titlesView.frame = CGRectMake(0, QhNavMaxY, QhScreenW, QhTitlesViewH);
    [self.view addSubview:titlesView];
    self.titlesView = titlesView;
    // 标题栏按钮
    [self createTitleButtons];
    // 标题下划线
    [self setupTitleUnderline];
}

- (void)createTitleButtons
{
    // 文字
    NSArray *titles = @[@"身边好店", @"离我最近", @"等级最高"];
    NSUInteger count = titles.count;
    
    // 标题按钮的尺寸
    CGFloat titleButtonW = self.titlesView.fsh_width / count;
    CGFloat titleButtonH = self.titlesView.fsh_height;
    
    // 创建5个标题按钮
    for (NSUInteger i = 0; i < count; i++) {
        QhTitleButton *titleButton = [[QhTitleButton alloc] init];
        titleButton.tag = i;
        [titleButton addTarget:self action:@selector(titleButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.titlesView addSubview:titleButton];
        // frame
        titleButton.frame = CGRectMake(i * titleButtonW, 0, titleButtonW, titleButtonH);
        // 文字
        [titleButton setTitle:titles[i] forState:UIControlStateNormal];
    }
}

- (void)setupTitleUnderline
{
    // 标题按钮
    QhTitleButton *firstTitleButton = self.titlesView.subviews.firstObject;
    
    // 下划线
    UIView *titleUnderline = [[UIView alloc] init];
    titleUnderline.fsh_height = 2;
    titleUnderline.fsh_y = self.titlesView.fsh_height - titleUnderline.fsh_height;
    titleUnderline.backgroundColor = [firstTitleButton titleColorForState:UIControlStateSelected];
    [self.titlesView addSubview:titleUnderline];
    self.titleUnderline = titleUnderline;
    
    // 切换按钮状态
    firstTitleButton.selected = YES;
    self.previousClickedTitleButton = firstTitleButton;
    
    [firstTitleButton.titleLabel sizeToFit]; // 让label根据文字内容计算尺寸
    self.titleUnderline.fsh_width = firstTitleButton.titleLabel.fsh_width + QhMarin;
    self.titleUnderline.fsh_centerX = firstTitleButton.fsh_centerX;
}

- (void)createstylistCollectionView
{
    self.collectionView.backgroundColor = QhCommonColor;
    [self.collectionView setContentInset:UIEdgeInsetsMake(QhTitlesViewH + QhMarin, 0, 0, 0)];
    self.collectionView.bounces = NO;
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.collectionView registerNib:[UINib nibWithNibName:@"QhStoreCell" bundle:nil] forCellWithReuseIdentifier:ID];
}
#pragma mark - Action
- (void)titleButtonClick:(QhTitleButton *)titleButton
{
    // 切换按钮状态
    self.previousClickedTitleButton.selected = NO;
    titleButton.selected = YES;
    self.previousClickedTitleButton = titleButton;
    
    [UIView animateWithDuration:0.25 animations:^{
        // 处理下划线
        self.titleUnderline.fsh_width = titleButton.titleLabel.fsh_width + QhMarin;
        self.titleUnderline.fsh_centerX = titleButton.fsh_centerX;
    } completion:^(BOOL finished) {
        
    }];
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 8;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    QhStoreCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    if (indexPath.row == 1) {
        cell.store.image = [UIImage imageNamed:@"fsh_store2"];
    } else if (indexPath.row == 2) {
        cell.store.image = [UIImage imageNamed:@"fsh_store3"];
    } else if (indexPath.row == 4) {
        cell.store.image = [UIImage imageNamed:@"fsh_store4"];
    }
    return cell;
}

@end
